package cn.edu.scau.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.scau.dao.CustomerMapper;
import cn.edu.scau.dao.GoOutRegisterMapper;
import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.GoOutRegister;
import cn.edu.scau.service.GoOutRegisterService;
@Service
public class GoOutRegisterServiceImpl implements GoOutRegisterService{

	@Autowired
	GoOutRegisterMapper goOutRegisterMapper;
	@Autowired
	CustomerMapper customerMapper;
	@Override
	public List<GoOutRegister> selectAll() {
		return goOutRegisterMapper.selectAll();
	}

	@Override
	public List<GoOutRegister> selectByCustomerId(Integer id) {
		return goOutRegisterMapper.selectByCustomerId(id);
	}

	@Override
	public List<GoOutRegister> find(Integer cid, String cname) {
		List<Customer> customers = customerMapper.findByIdName(cid, cname);
		List<GoOutRegister> goOutRegisters = new ArrayList<GoOutRegister>();
		for(Customer c : customers) {
			goOutRegisters.addAll(goOutRegisterMapper.selectByCustomerId(c.getCustomerid()));
		}
		return goOutRegisters;
	}

	@Override
	public boolean updateGoOutRegisterByPrimaryKey(Date gooutdate, Date expectedbackdate) {
		GoOutRegister record = goOutRegisterMapper.selectByPrimaryKey(gooutdate, expectedbackdate);
		Date thisDate = new Date();
		record.setActuallybackdate(thisDate);
		
		return 1==goOutRegisterMapper.updateByPrimaryKey(record);
	}

	@Override
	public boolean insert(GoOutRegister record) {
		return 1==goOutRegisterMapper.insertSelective(record);
	}

}
